//
//  SWPhotoPublishViewController.swift
//  ChildStory
//
//  Created by linhan on 16/8/19.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWPhotoPublishViewController: SWBaseViewController
{
    var image:UIImage
    var successCallback:((String)->Void)?
    
    private var _imageView:UIImageView = UIImageView()
    private var _checkmark:UIImageView = UIImageView(image:UIImage(named:"checkin_icon_success"))
    
    
    init(image:UIImage) {
        self.image = image
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillLayoutFirstTime()
    {
        setup()
    }
    
    private func setup()
    {
        createNavigationRightButton("上传", target: self, action: #selector(uploadBtnTapped))
        
        _imageView.frame = CGRectMake(0, navigationBarBottom, view.width, view.width)
        _imageView.contentMode = .scaleAspectFit
        _imageView.image = image
        view.addSubview(_imageView)
        
        /**
        var attributes:[(imageName:String, label:String)] = []
        attributes.append((imageName: "magenta", label:"大喜"))
        attributes.append((imageName: "pink", label:"愉悦"))
        attributes.append((imageName: "yellow", label:"平和"))
        attributes.append((imageName: "green", label:"不开心"))
        attributes.append((imageName: "cyan", label:"大怒"))
        attributes = attributes.reverse()
        
        let colorfulBtnContainer:UIView = UIView()
        colorfulBtnContainer.frame = CGRectMake(0, _imageView.bottom + 10, view.width, 100)
        view.addSubview(colorfulBtnContainer)
        
        
        var buttons:[UIButton] = []
        for (index, attribute) in attributes.enumerate()
        {
            let button = createButton(index, attribute: attribute)
            colorfulBtnContainer.addSubview(button)
            buttons.append(button)
        }
        
        LayoutUtil.distributeHAverageABS(buttons, containerWidth: colorfulBtnContainer.width)
        
        _checkmark.isHidden = true
        colorfulBtnContainer.addSubview(_checkmark)
        **/
    }
    
    private func createButton(index:Int, attribute:(imageName:String, label:String)) -> UIButton
    {
        let size:CGSize = CGSizeMake(60, 60)
        let button = ButtonUtil.createTextImageVerticalButton(attribute.label, imageSize: size, fontSize: 14, gap: 14)
        button.setImage(UIImage(named: attribute.imageName), for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.tag = index
        button.addTarget(self, action: #selector(colorfulBtnTapped(_:)), for: .touchUpInside)
        return button
    }
    
    private func close()
    {
        dismiss(animated:true, completion: nil)
    }
    
    @objc private func colorfulBtnTapped(_ button:UIButton)
    {
        _checkmark.isHidden = false
        _checkmark.center = button.center
    }
    
    private let uploader:SWImageUploader = SWImageUploader()
    @objc private func uploadBtnTapped()
    {
        SVProgressHUD.show(withStatus:"保存中...")
        uploader.upload(image: image, quality: 0.6, successCallback: {[weak self] url in
            SVProgressHUD.showSuccess(withStatus:"图片已保存")
            if let strongSelf = self{
                UIImageWriteToSavedPhotosAlbum(strongSelf.image, nil, nil, nil)
                strongSelf.successCallback?(url)
            }
            
            },failCallback:{
                SVProgressHUD.showError(withStatus:"图片保存失败")
        })
    }
    
}
